package com.nefu.huiyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nefu.huiyi.entity.City;
import com.nefu.huiyi.mapper.CityMapper;
import com.nefu.huiyi.service.CityService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements CityService {

    @Override
    public City saveCity(City city) {
        save(city);
        return city;
    }

    @Override
    public boolean deleteCity(Integer id) {
        return removeById(id);
    }

    @Override
    public City updateCity(City city) {
        updateById(city);
        return city;
    }

    @Override
    public City getCityById(Integer id) {
        return getById(id);
    }

    @Override
    public List<City> findAll() {
        return list();
    }

    @Override
    public IPage<City> getCityPage(Page<City> page, String cityName, String province) {
        LambdaQueryWrapper<City> wrapper = new LambdaQueryWrapper<>();
        if (cityName != null && !cityName.isEmpty()) {
            wrapper.like(City::getCityName, cityName);
        }
        if (province != null && !province.isEmpty()) {
            wrapper.eq(City::getProvince, province);
        }
        return page(page, wrapper);
    }

    @Override
    public List<City> getCitiesByPharmaCount(Integer minCount) {
        LambdaQueryWrapper<City> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(City::getPharmaCompanyCount, minCount)
                .orderByDesc(City::getPharmaCompanyCount);
        return list(wrapper);
    }
}